<template>
  <div class="login">
    <div class="title">温州市管理平台</div>
    <div class="content">
      <a-form-model
        id="formLogin"
        ref="formLogin"
        :model="form"
        :rules="rules"
        @submit="handleSubmit"
      >
        <a-row :gutter="16">
          <a-col :span="24">
            <a-form-model-item prop="currentDataScope">
              <a-select
                v-model="form.currentDataScope"
                size="large"
                placeholder="请选择"
              >
                <a-select-option
                  v-for="item in dataScope"
                  :key="item.dictKey"
                  :value="item.dictKey"
                >
                  {{ item.dictValue }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="24">
            <a-form-model-item prop="userName">
              <a-input
                size="large"
                type="text"
                placeholder="账户"
                v-model="form.userName"
              >
                <a-icon
                  slot="prefix"
                  type="user"
                  :style="{ color: 'rgba(0,0,0,.25)' }"
                />
              </a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="24">
            <a-form-model-item prop="userPassword">
              <a-input
                size="large"
                type="password"
                autocomplete="false"
                placeholder="密码"
                v-model="form.userPassword"
              >
                <a-icon
                  slot="prefix"
                  type="lock"
                  :style="{ color: 'rgba(0,0,0,.25)' }"
                />
              </a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :span="15">
            <a-form-model-item prop="imgCode">
              <a-input
                size="large"
                type="text"
                placeholder="图片验证码"
                v-model="form.imgCode"
              >
                <a-icon
                  slot="prefix"
                  type="picture"
                  :style="{ color: 'rgba(0,0,0,.25)' }"
                />
              </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="9">
            <img :src="imgSrc" @click="initCode" mode="" class="getCaptcha" />
          </a-col>
        </a-row>

        <a-row :gutter="16">
          <a-col :span="24">
            <div style="padding-top: 5px">
              <a-button
                size="large"
                type="primary"
                htmlType="submit"
                class="login-button"
                >登<span style="width: 20px"></span>录</a-button
              >
            </div>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
  </div>
</template>
<script>
import md5 from "md5";
import { mapActions } from "vuex";
import { getBrowerInfo, uuid } from "@/utils/util";
import { message } from "ant-design-vue";
export default {
  name: "Login",
  data() {
    return {
      form: {},
      rules: {
        currentDataScope: [{ required: true, message: "请选择平台" }],
        userName: [{ required: true, message: "请输入用户名" }],
        userPassword: [{ required: true, message: "请输入密码" }],
        imgCode: [{ required: true, message: "请输入图片验证码" }],
      },
      dataScope: [],
      loginKey: uuid(),
      imgSrc: null,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.initCode();
    this.getDataScope();
  },
  methods: {
    ...mapActions(["Login", "Logout"]),
    getDataScope() {
      this.dataScope = [
        { dictKey: "busiSuperAdmin", dictValue: "超级管理员平台" },
        { dictKey: "330300000000", dictValue: "温州市数据区平台" },
        { dictKey: "330302", dictValue: "鹿城区数据区平台" },
        { dictKey: "330303", dictValue: "龙湾区数据区平台" },
        { dictKey: "330304", dictValue: "瓯海区数据区平台" },
        { dictKey: "330305", dictValue: "洞头区数据区平台" },
        { dictKey: "330382", dictValue: "乐清市数据区平台" },
        { dictKey: "330381", dictValue: "瑞安市数据区平台" },
        { dictKey: "330383", dictValue: "龙港市数据区平台" },
        { dictKey: "330329", dictValue: "泰顺县数据区平台" },
        { dictKey: "330324", dictValue: "永嘉县数据区平台" },
        { dictKey: "330326", dictValue: "平阳县数据区平台" },
        { dictKey: "330327", dictValue: "苍南县数据区平台" },
        { dictKey: "330328", dictValue: "文成县数据区平台" },
      ];
    },
    initCode() {
      const url = `${
        process.env.VUE_APP_API_BASE_URL
      }/sso/common/getImgCode?loginKey=${
        this.loginKey
      }&time=${new Date().getTime()}`;
      this.imgSrc = url;
      this.$set(this.form, "imgCode", "");
    },
    handleSubmit(e) {
      e.preventDefault();
      let form = { ...this.form };
      if (!form.currentDataScope) {
        message.warning("请选择平台");
        return;
      }
      if (!form.userName) {
        message.warning("请输入账户");
        return;
      }
      if (!form.userPassword) {
        message.warning("请输入密码");
        return;
      }
      if (!form.imgCode) {
        message.warning("请输入验证码");
        return;
      }
      form.userPassword = md5(form.userPassword);
      form.browser = getBrowerInfo().browserType;
      form.equipmentType = "pc";
      form.loginType = "username";
      form.userType = "10";
      form.loginKey = this.loginKey;
      this.Login(form)
        .then(() => {
          this.$router.push('/');
        })
        .catch((err) => {
          message.error(err.message || err.msg);
        });
    },
  },
};
</script>
  
<style  lang="scss" scoped>
.login {
  height: 100%;
  background-image: url("../assets/img/login-bg.png");
  background-size: 100% 100%;
  color: #333;
  .title {
    position: absolute;
    top: 18%;
    left: 15%;
    font-size: 50px;
    font-weight: bold;
    color: #00d8ff;
  }
  .content {
    position: absolute;
    top: 50%;
    right: 150px;
    width: 400px;
    height: 360px;
    transform: translate(0, -50%);
    background: #ffffff;
    --ShadowSize: 0px 20px 50px 0px;
    border-radius: 8px;
    padding: 35px;
  }
  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
    cursor: pointer;
  }
  .login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }
}
</style>